<template>
  <view class="contentBd">
    <scroll-view scroll-x class="tabList">
      <view
        @click="$emit('handel', 'tab', item)"
        :class="['item', active === item.tab ? 'active' : '']"
        v-for="(item, index) of tab"
        :key="index"
      >
        {{ item.tab }}
      </view>
    </scroll-view>

    <view
      class="popup-list-item"
      v-for="(sku, i) in items"
      :key="i"
      @click="$utils.toUrl(`/goods/goods/goods?id=${sku.id}`)"
    >
      <view class="good-info">
        <view class="goodsNew" v-if="newShow(sku)">新人专享</view>
        <image class="good-pic" :src="sku.cover_pic" mode="" />
        <view class="info-section">
          <view class="good-name">{{ sku.name }}</view>
          <view class="good-num"
            >{{ $t("shop.month") }} {{ sku.virtual_sales || 0 }}</view
          >
          <view class="good-select">
            <view class="good-price">
              <text>￥</text>
              <text>{{ sku.price | priceBeforeZero }}</text>
              <text>{{ sku.price | priceAfterZero }}</text>
            </view>

            <template v-if="sku.product_type == 0">
              <view class="action-info" v-if="sku.use_attr == 0">
                <view class="add" @click.stop="$emit('add', sku)">
                  <i class="iconfont icon-add-select"></i>
                </view>
              </view>
              <view class="choose-sku" v-else @click.stop="$emit('add', sku)">
                <text>{{ $t("alipay.sku") }}</text>
                <view v-if="sku.select_num > 0" class="attr-select-num">{{
                  sku.select_num
                }}</view>
              </view>
            </template>
          </view>
        </view>
      </view>
    </view>
  </view>
</template>

<script>
export default {
  props: {
    tab: {
      type: Array,
      default: () => {
        return [];
      },
    },

    items: {
      type: Array,
      default: () => {
        return [];
      },
    },

    active: {
      type: String,
      default: "",
    },

    newGoods: {
      type: String,
      default: "",
    },
  },

  filters: {
    priceBeforeZero(val) {
      if (!val) val = "";
      if (val.indexOf(".") < 0) return `${val}.`;
      return val.substr(0, val.indexOf("."));
    },
    priceAfterZero(val) {
      if (!val) val = "";
      if (val.indexOf(".") < 0) return "00";
      return val.substr(val.indexOf("."), val.length);
    },
  },

  computed: {
    newShow() {
      return ({ id }) => {
        return this.newGoods.split(",").find((el) => el === id);
      };
    },
  },

  data() {
    return {};
  },

  async onLoad() {
    const confRes = await newUserConf();
    if (!confRes.code) {
      this.conf = confRes.data.conf;
    }

    const infoRes = await info();
    if (!infoRes.code) {
      this.couponList = infoRes.coupon_list || [];

      if (infoRes.data.goods_ids) {
        this.goods_ids = infoRes.data.goods_ids;
        this.getGoodsList({ goods_id: infoRes.data.goods_ids }, 1);
      }
    }

    const goodsRes = await newUserPushGoods();
    if (!goodsRes.code) {
      this.tab = goodsRes.data.conf || [];

      if (this.tab.length) {
        this.handel("tab", this.tab[0]);
      }
    }
  },

  methods: {
    handel(type, item) {
      switch (type) {
        case "tab":
          if (this.active !== item.tab) {
            this.active = item.tab;
            if (item.cat_ids) {
              this.getGoodsList({ cat_id: item.cat_ids });
            } else {
              this.getGoodsList({ goods_id: item.goods_ids.join(",") });
            }
          }
          break;
      }
    },

    async getGoodsList(params, type = 0) {
      let res = await this.$allrequest.diy.getGoodsList(params, true);

      if (!res.code) {
        if (!type) {
          this.goods = res.data.list || [];
        } else {
          this.goodsList = res.data.list || [];
        }
      }
    },
  },
};
</script>

<style lang="less" scoped>
.contentBd {
  padding: 24rpx;
}

.tabList {
  .item {
    display: inline-block;
    margin-right: 24rpx;
    vertical-align: middle;
    width: 148rpx;
    height: 56rpx;
    line-height: 56rpx;
    text-align: center;
    font-size: 24rpx;
    color: #262626;
    border-radius: 32px;
    background: #fff;

    &.active {
      background: #f60313;
      color: #fff;
    }
  }
}

.popup-list-item {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  margin-top: 24rpx;
  padding: 28rpx 26rpx;
  background: #fff;
  border-radius: 24rpx;
  // margin-bottom: 6.4vw;

  // width: 100%;

  .good-info {
    position: relative;
    width: 100%;
    display: flex;
    align-items: flex-start;
    .good-pic {
      width: 166rpx;
      height: 166rpx;
      background: #f2f2f2;
      border-radius: 10rpx;
      display: block;
      margin-right: 20rpx;
      flex-shrink: 0;
    }
    .info-section {
      flex: 1;
      overflow: hidden;

      .good-name {
        font-size: 28rpx;
        font-family: PingFang SC;
        font-weight: 600;
        line-height: 40rpx;
        color: #262626;
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
      }
      .good-sku {
        max-width: 40.27vw;
        font-size: 3.2vw;
        font-weight: 400;
        line-height: 4.27vw;
        color: #999999;
        margin-top: 1.07vw;
        overflow: hidden;
        white-space: nowrap;
        text-overflow: ellipsis;
      }
      .good-num {
        font-size: 24rpx;
        font-weight: 400;
        color: #8c8c8c;
        margin-top: 10rpx;
      }
      .good-select {
        display: flex;
        justify-content: space-between;
        align-items: center;
        margin-top: 30rpx;
      }
      .good-price {
        display: flex;
        align-items: baseline;
        font-size: 32rpx;
        font-weight: 600;
        color: #262626;

        text:nth-child(2) {
          font-size: 32rpx;
        }
      }
    }
  }
  .action-info {
    display: flex;
    align-items: center;
    .reduce {
      width: 44rpx;
      height: 44rpx;
      background: #ffffff;
      border-radius: 16rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      border: 1px solid #f60313;
      view {
        width: 24rpx;
        height: 4rpx;
        background: #f0250e;
      }
    }
    .disabled {
      background: #f1f1f1;
      border: 0 none;
      view {
        background: #ffffff;
      }
    }
    .num {
      font-size: 3.73vw;
      font-weight: 400;
      line-height: 4.27vw;
      color: #191919;
      margin: 0 2.93vw;
    }
    .add {
      width: 44rpx;
      height: 44rpx;
      background: #f60313;
      border-radius: 16rpx;
      display: flex;
      justify-content: center;
      align-items: center;
      .iconfont {
        color: #ffffff;
        font-weight: bold;
        font-size: 36rpx;
      }
    }
  }
  .choose-sku {
    width: 100rpx;
    height: 44rpx;
    background: #f60313;
    border-radius: 16rpx;
    text-align: center;
    line-height: 44rpx;
    font-size: 24rpx;
    font-weight: 600;
    color: #ffffff;
    position: relative;
    .attr-select-num {
      width: 32rpx;
      height: 32rpx;
      background: #f84a33;
      border-radius: 50%;
      display: flex;
      justify-content: center;
      align-items: center;
      font-size: 20rpx;
      font-family: PingFang SC;
      font-weight: 600;
      color: #ffffff;
      position: absolute;
      right: -16rpx;
      top: -16rpx;
    }
  }
}
.popup-list-item:last-child {
  margin-bottom: 0;
}

.imgBg {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}

.goodsNew {
  position: absolute;
  left: -4rpx;
  top: -4rpx;
  padding: 2rpx 10rpx;
  background: linear-gradient(90deg, #ff5d33 0%, #f63948 100%);
  border-radius: 8rpx;
  font-size: 20rpx;
  color: #fff;
}
</style>
